﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

namespace ScoreMS.Views
{
    /// <summary>
    /// OutputDetailScore.xaml 的交互逻辑
    /// </summary>
    public partial class OutputDetailScore : Page
    {
        public OutputDetailScore()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(OutputDetailScore_Loaded);
        }

        void OutputDetailScore_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbxCourse.ItemsSource = BLL.CourseBS.GetCourseList(App.teacher_id);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (cbxCourse.SelectedIndex == -1 || cbxClass.SelectedIndex == -1)
                {
                    MessageBox.Show("请填写完整！", "提示");
                    return;
                }
                FileStream file = new FileStream(@"Template/01详细成绩表.xls", FileMode.Open, FileAccess.Read);
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
                ISheet sheet1 = hssfworkbook.GetSheetAt(hssfworkbook.ActiveSheetIndex);

                var course_id = (cbxCourse.SelectedItem as Model.Course).course_id;
                var course_name = (cbxCourse.SelectedItem as Model.Course).course_name;
                var course_points = (cbxCourse.SelectedItem as Model.Course).course_points;
                var class_id = (cbxClass.SelectedItem as Model.Class).class_id;
                var teacher_name = BLL.TeatherBS.GetTeacherInfo(App.teacher_id).teacher_name;
                if (BLL.IO.Generate(hssfworkbook, sheet1, class_id, course_name, teacher_name, course_points
                    , BLL.Score_finalBS.GetScore_finalList(course_id, class_id)))
                {
                    MessageBox.Show("输出成功！输出文档位于程序Output目录下！", "提示");
                }
                else
                {
                    MessageBox.Show("操作失败！", "警告");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("操作失败！" + ex.ToString(), "警告");
            }
        }

        private void cbxCourse_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxCourse.SelectedIndex == -1) return;
            var course_id = (cbxCourse.SelectedItem as Model.Course).course_id;
            this.cbxClass.ItemsSource = BLL.ClassBS.GetClassList(course_id);
        }
    }
}
